﻿using AnyLink.Domain.Entities.System;
using AnyLink.Infrastructure.Repositories;

namespace AnyLink.Infrastructure.Services
{
    public class SysConfigService : BaseService<SysConfig>
    {
        public SysConfigService(ISqlSugarRepository db) : base(db)
        {
        }
        /// <summary>
        /// 根据参数ID查询参数信息
        /// </summary>
        /// <param name="configId"></param>
        /// <returns></returns>
        public SysConfig SelectConfigById(long configId)
        {
            return base.FirstOrDefault(n => n.Id == configId);            
        }
        /// <summary>
        /// 根据参数键名查询参数值
        /// </summary>
        /// <param name="configKey"></param>
        /// <returns></returns>
        public string SelectConfigByKey(String configKey)
        {
            /*String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
            if (StringUtils.isNotEmpty(configValue))
            {
                return configValue;
            }
            SysConfig config = new SysConfig();
            config.setConfigKey(configKey);
            SysConfig retConfig = configMapper.selectConfig(config);
            if (StringUtils.isNotNull(retConfig))
            {
                redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
                return retConfig.getConfigValue();
            }
            return StringUtils.EMPTY;*/
            return base.FirstOrDefault(n => n.ConfigKey == configKey)?.ConfigValue;
        }
        /// <summary>
        /// 验证码开关
        /// </summary>
        /// <returns></returns>
        public bool SelectCaptchaEnabled()
        {
            string captchaEnabled = SelectConfigByKey("sys.account.captchaEnabled");
            if (string.IsNullOrWhiteSpace(captchaEnabled))
            {
                return true;
            }
            return Convert.ToBoolean(captchaEnabled);
        }
    }
}
